<?php
mysql_connect('localhost','root','techuso')or die ('Ha fallado la conexión: '.mysql_error());
mysql_select_db('condosabana')or die ('Error al seleccionar la Base de Datos: '.mysql_error());
// Connection error
class connectionError extends Exception{};

// MySQLi error
class MySQLiError extends Exception{};

// No results in a query
class noResults extends Exception{};

// Query Manager Class
class queryManager
{
	
	private $mysqli;
	private $query;
	private $result = array();

	// Database connection
	function __construct()
	{
		if(!$this->mysqli = new mysqli('localhost','root','techuso','condosabana'))
			throw new connectionError;
	}

	// Insert a row into a table
	public function insert($table, $arrayInsert)
	{
		$column = '';
		$value = '';

		foreach($arrayInsert as $col => $val)
		{
			$column .= $col;
			$value .= '"'.$val.'"';

			if($i < count($arrayInsert) - 1)
			{
				$column .= ',';
				$value .= ',';
			}

			$i++;
		}

		if(!$this->mysqli->query('INSERT INTO '.$table.' ('.$column.') VALUES ('.$value.')'))
			throw new MySQLiError;
		else
			return $this->mysqli->insert_id;
	}

	// Update a row from a table
	public function update($table, $arrayUpdate, $where)
	{
		$update = '';
		$i = 0;

		foreach($arrayUpdate as $column => $value)
		{
			$update .= $column.' = "'.$value.'"';
			$update .= ($i < count($arrayUpdate) - 1) ? ', ' : '';
			$i++;
		}

		if(!$this->mysqli->query('UPDATE '.$table.' SET '.$update.' WHERE '.$where))
			throw new MySQLiError;
	}

	// Select from a table
	public function select($params = '*', $table = '', $column = '', $value = '', $order = '', $limit = '')
	{
		if($column != '' && $value != '')
		{
			$column = explode(',',$column);
			$value = explode(',',$value);
			$where = 'WHERE ';
	
			for($i = 0; $i <= count($column) - 1; $i++)
			{
				$where .= $column[$i].' = "'.$this->mysqli->real_escape_string($value[$i]).'"';
				$where .= ($i == count($column) - 1) ? '' : ' && ';
			}
		}
		mysql_set_charset("UTF8");
		$this->query = $this->mysqli->query('SELECT '.$params.' FROM '.$table.' '.$where.' '.$order.' '.$limit);

		if(!$this->query->num_rows)
			throw new noResults;
	}

	// Advanced select
	public function advancedSelect($table, $arraySelect)
	{
		if(!empty($arraySelect))
		{
			$where = 'WHERE ';
			$i = 0;

			foreach($arraySelect as $key => $array)
			{
				$where .= $array[0].' '.$array[1].' '.$array[2].'';
				$where .= ($i < count($arraySelect) - 1) ? ' && ' : '';
				$i++;
			}
		}
		else
		{
			$where = '';
		}
		mysql_set_charset("UTF8");
		$this->query = $this->mysqli->query('SELECT * FROM '.$table.' '.$where);

		if(!$this->query->num_rows)
			throw new noResults;
	}

	// Get object from a select query
	public function fetchObject()
	{
		while($row = $this->query->fetch_object())
			$this->result[] = $row;
		mysql_set_charset("UTF8");
		return $this->result;
	}

	// Delete a row from a table
	public function delete($table, $arrayWhere)
	{
		$where = '';
		$i = 0;

		foreach($arrayWhere as $column => $value)
		{
			$where .= $column.' = "'.$value.'"';
			$where .= ($i < count($arrayWhere) - 1) ? ' && ' : '';
			$i++;
		}

		if(!$this->mysqli->query('DELETE FROM '.$table.' WHERE '.$where))
			throw new MySQLiError;
	}
}
?>